/*
 * EventListener.h
 *
 *  Created on: 24 nov 2011
 *      Author: Robert
 *
 *
 * It is a shame that you cannot have the template class the same name as the base class. Right?
 *
 * When instantiating a listener you need to do something like this:
 *
 *   EventListenerT<Observer>(observer, &Observer::handleEventOneA);
 *
 * Which feels redundant. Function templates can deduce the type themselves
 * so we could potentially have a "factory":
 *
 *   template<typename T>
 *   EventListenerT<T> Listener(T &object, void (T::*function)(const Event&)) {
 *   	return EventListenerT<T>(object, function);
 *   }
 *
 *   Listener(observer, &Observer::handleEventOneA);
 *
 * Now, if one could get rid of the Observer:: as well, and figure our a non-conflicting name...
 */

#ifndef EVENTLISTENER_H_
#define EVENTLISTENER_H_


class Event;


class EventListener
{
	public:
		EventListener() {}
		virtual ~EventListener() {}
		virtual void operator()(const Event &event) const = 0;
};


template<typename T>
class EventListenerT : public EventListener
{
public:
	typedef void (T::*MemberFunction)(const Event &event);

	EventListenerT(T &object, MemberFunction function) : object(object), function(function) {}
	void operator()(const Event &event) const {
		(object.*function)(event);
	}
protected:
	T &object;
	MemberFunction function;
};


#endif /* EVENTLISTENER_H_ */
